package com.liuda.workbench.controller;

import com.liuda.settings.domain.User;
import com.liuda.settings.service.UserService;
import com.liuda.workbench.domain.Contact;
import com.liuda.workbench.domain.Customer;
import com.liuda.workbench.domain.CustomerRemark;
import com.liuda.workbench.domain.Transfer;
import com.liuda.workbench.service.ContactService;
import com.liuda.workbench.service.CustomerRemarkService;
import com.liuda.workbench.service.CustomerService;
import com.liuda.workbench.service.TransferService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.ResourceBundle;

@Controller
public class CustomerController {
    @Autowired
    CustomerService customerService;
    @Autowired
    CustomerRemarkService customerRemarkService;
    @Autowired
    TransferService transferService;
    @Autowired
    ContactService contactService;
    @Autowired
    UserService userService;
    @RequestMapping("/workbench/customer/toIndex.do")
    public String toIndex(HttpServletRequest request){
        List<Customer> customerList= customerService.selectAllForDetail();
        List<User> userList=userService.getAllUnlockedUsers();
        request.setAttribute("customerList",customerList);
        request.setAttribute("userList",userList);
        return "workbench/customer/index";
    }
    @RequestMapping("/workbench/customer/toDetail.do")
    public String toDetail(String id,HttpServletRequest request){
        Customer customer=customerService.selectForDetailById(id);
        List<CustomerRemark> customerRemarkList=customerRemarkService.selectByCustomerId(id);
        List<Transfer> transactionList =transferService.selectByCustomerId(id);
        ResourceBundle bundle=ResourceBundle.getBundle("possibility");
        for (Transfer transfer : transactionList) {
            transfer.setPossibility(bundle.getString(transfer.getStage()));
        }
        List<Contact> contactList=contactService.selectByCustomerId(id);
        request.setAttribute("customer",customer);
        request.setAttribute("customerRemarkList",customerRemarkList);
        request.setAttribute("transactionList",transactionList);
        request.setAttribute("contactList",contactList);
        return "workbench/customer/detail";
    }
}
